COVID-19 Data Visualization

In [1]:
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import dash_table_experiments as dt
import dash.dependencies
import pandas as pd
import plotly.graph_objs as go
import plotly.express as px
import plotly.offline as py
In [2]:
covid19=pd.read_csv("covid_19.csv")
covid19.head()
Out[2]:
SNo ObservationDate Province/State Country/Region Last Update Confirmed Deaths Recovered
0 1 01/22/2020 Anhui Mainland China 1/22/2020 17:00 1.0 0.0 0.0
1 2 01/22/2020 Beijing Mainland China 1/22/2020 17:00 14.0 0.0 0.0
2 3 01/22/2020 Chongqing Mainland China 1/22/2020 17:00 6.0 0.0 0.0
3 4 01/22/2020 Fujian Mainland China 1/22/2020 17:00 1.0 0.0 0.0
4 5 01/22/2020 Gansu Mainland China 1/22/2020 17:00 0.0 0.0 0.0
In [3]:
covid19=covid19.fillna('NoState')
# Change the data tye
num_cols=['Confirmed', 'Deaths', 'Recovered']
for col in num_cols:
    temp=[int(i) for i in covid19[col]]
    covid19[col]=temp
    
covid19.groupby(['Country/Region','Confirmed']).sum()
# Creating list of all regions of all counntries
test=covid19
uniq_reg=test['Country/Region'].unique()
states=[]
for reg in uniq_reg:
    states.append(test[test['Country/Region']==reg]['Province/State'].unique())  

# Total Confirmed cases per conutry

confirmcase=[]
for i in range(len(uniq_reg)):
    count=0
    covid_temp=covid19[covid19['Country/Region']==uniq_reg[i]]
    for state in states[i]:
        #print(state)
        count+=covid_temp[covid_temp['Province/State']==state].sort_values('ObservationDate').iloc[-1]['Confirmed']
    confirmcase.append(count)

    
## Total Deaths cases per conutry
total_deaths=[]
for i in range(len(uniq_reg)):
    count=0
    covid_temp=covid19[covid19['Country/Region']==uniq_reg[i]]
    for state in states[i]:
        #print(state)
        count+=covid_temp[covid_temp['Province/State']==state].sort_values('ObservationDate').iloc[-1]['Deaths']
    total_deaths.append(count)
In [4]:
countrywise=pd.DataFrame(columns=['country','confirmed','deaths'],index=None)
countrywise.country=uniq_reg
countrywise.confirmed=confirmcase
countrywise.deaths=total_deaths
countrywise.to_csv('countrywise.csv')
countrywise.head()
Out[4]:
country confirmed deaths
0 Mainland China 82880 4633
1 Hong Kong 1039 4
2 Macau 45 0
3 Taiwan 483 7
4 US 1158728 67705
In [5]:
data=countrywise.sort_values('confirmed',ascending=False)
#fig = px.treemap(data, path=['country'], values='confirmed',
   #               color='confirmed', hover_data=['country'],
    #              color_continuous_scale='Rainbow')
#fig.show()
In [6]:
#Distribution of confirmed and death cases around the globe 

from plotly.subplots import make_subplots
data=countrywise.sort_values('confirmed',ascending=False)
data.loc[data['confirmed'] < 20000, 'country'] = 'Other countries'
data.loc[data['deaths'] < 1000, 'country'] = 'Other countries'
labels=list(data['country'])

fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])
fig.add_trace(go.Pie(labels=labels, values=list(data['confirmed']), name='country'),
              1, 1)
fig.add_trace(go.Pie(labels=labels, values=list(data['deaths']), name='country'),
              1, 2)
fig.update_traces(hole=.4, hoverinfo="label+percent+name",textposition='inside',textfont_size=12)

fig.update_layout(
    title_text="Distribution of confirmed and death cases around the globe.",
 
    annotations=[dict(text='Confirm', x=0.18, y=0.5, font_size=14, showarrow=False),
                 dict(text='Deaths', x=0.82, y=0.5, font_size=14, showarrow=False)])
fig.show()
In [7]:
#Analysis of death and Infection around the globe.


dateandcountrywise = covid19.groupby(['ObservationDate', 'Country/Region'])['Confirmed', 'Deaths', 'Recovered'].max().reset_index()

figch = px.choropleth(dateandcountrywise, locations="Country/Region", locationmode='country names', 
                     color="Confirmed", hover_name="Country/Region",hover_data = [dateandcountrywise.Confirmed,dateandcountrywise.Deaths,dateandcountrywise.Recovered],projection="mercator",
                     animation_frame="ObservationDate",width=950, height=700,
                     color_continuous_scale='Reds',
                     range_color=[2000,6000],

                     title='Analysis of death and Infection around the globe.')

figch.update(layout_coloraxis_showscale=True)
figch.update_layout(
    autosize=False,
    width=1300,
    height=800)
#py.offline.iplot(figch)
In [8]:
# patient death ration according age group and gender
covid19age_sex=pd.read_csv("Covid19agegroup.csv")
covid19age_sex['birth_year'] = covid19age_sex['birth_year'] .fillna(0.0)
# getting age of patient
covid19age_sex['age'] = 2020 - covid19age_sex['birth_year'] 
bins = [18, 30, 40, 50, 60, 70, 120]
labels = ['18-29', '30-39', '40-49', '50-59', '60-69', '70+']
covid19age_sex['agerange'] = pd.cut(covid19age_sex.age, bins, labels = labels,include_lowest = True)
covid19age_sex.head()
Out[8]:
patient_id global_num sex birth_year age country province city disease infection_case infection_order infected_by contact_number symptom_onset_date confirmed_date released_date deceased_date state agerange
0 1000000001 2.0 male 1964.0 56.0 Korea Seoul Gangseo-gu NaN overseas inflow 1.0 NaN 75.0 2020-01-22 2020-01-23 2020-02-05 NaN released 50-59
1 1000000002 5.0 male 1987.0 33.0 Korea Seoul Jungnang-gu NaN overseas inflow 1.0 NaN 31.0 NaN 2020-01-30 2020-03-02 NaN released 30-39
2 1000000003 6.0 male 1964.0 56.0 Korea Seoul Jongno-gu NaN contact with patient 2.0 2.002000e+09 17.0 NaN 2020-01-30 2020-02-19 NaN released 50-59
3 1000000004 7.0 male 1991.0 29.0 Korea Seoul Mapo-gu NaN overseas inflow 1.0 NaN 9.0 2020-01-26 2020-01-30 2020-02-15 NaN released 18-29
4 1000000005 9.0 female 1992.0 28.0 Korea Seoul Seongbuk-gu NaN contact with patient 2.0 1.000000e+09 2.0 NaN 2020-01-31 2020-02-24 NaN released 18-29
In [9]:
# patient death ration according age group and gender
patient_dead = covid19age_sex[covid19age_sex.state == 'deceased']
male_death = patient_dead[patient_dead.sex=='male']
female_death = patient_dead[patient_dead.sex=='female']
In [10]:
sorted_pateient=patient_dead.sort_values("agerange", axis = 0, ascending = False,inplace=False, kind='quicksort', na_position='last')

figage_gender = px.histogram(sorted_pateient, x="agerange", color="sex",  title='Breakdown of the COVID 19 fatalities by age and gender wise. COVID 19 has had adverse effect on the elderly population.')
figage_gender.show()
In [11]:
# Hospital bed per 1000 people
beds=pd.read_csv("hospitalbeds.csv",engine='python')
data = beds.rename(
        columns = {'Country Name':'country'
                   ,'Country Code':'countrycode'
                   ,'Indicator Name':'Indname'
                   ,'Indicator Code':'Indcode'
                   }
        )

data = data.melt(id_vars = ['country','countrycode','Indname','Indcode']
                 ,var_name = 'year_RAW'
                 ,value_name = 'totalbed'
                 )
In [12]:
data['totalbed'] = data['totalbed'] .fillna(0)
In [13]:
beddata = data.groupby(['year_RAW', 'country'])['totalbed'].max().reset_index()
array = ['India', 'China','United States','Indonesia','Pakistan','Brazil','Nigeria','Bangladesh','Russia','Mexico','japan','Germany',
        'Russian Federation','France','United Kingdom','Turkey','Vietnam']
beddata = beddata[beddata.country.isin(array)]
beddata
Out[13]:
year_RAW country totalbed
16 1960 Bangladesh 0.000000
27 1960 Brazil 3.201795
44 1960 China 0.000000
81 1960 France 0.000000
86 1960 Germany 10.500000
109 1960 India 0.458987
110 1960 Indonesia 0.740457
155 1960 Mexico 0.000000
176 1960 Nigeria 0.418167
186 1960 Pakistan 0.557262
242 1960 Turkey 1.700000
249 1960 United Kingdom 10.700000
250 1960 United States 9.200000
256 1960 Vietnam 0.000000
279 1961 Bangladesh 0.000000
290 1961 Brazil 0.000000
307 1961 China 0.000000
344 1961 France 0.000000
349 1961 Germany 0.000000
372 1961 India 0.000000
373 1961 Indonesia 0.000000
418 1961 Mexico 0.000000
439 1961 Nigeria 0.000000
449 1961 Pakistan 0.000000
505 1961 Turkey 0.000000
512 1961 United Kingdom 0.000000
513 1961 United States 0.000000
519 1961 Vietnam 0.000000
542 1962 Bangladesh 0.000000
553 1962 Brazil 0.000000
... ... ... ...
15241 2017 United States 0.000000
15247 2017 Vietnam 0.000000
15270 2018 Bangladesh 0.000000
15281 2018 Brazil 0.000000
15298 2018 China 0.000000
15335 2018 France 0.000000
15340 2018 Germany 0.000000
15363 2018 India 0.000000
15364 2018 Indonesia 0.000000
15409 2018 Mexico 0.000000
15430 2018 Nigeria 0.000000
15440 2018 Pakistan 0.000000
15496 2018 Turkey 0.000000
15503 2018 United Kingdom 0.000000
15504 2018 United States 0.000000
15510 2018 Vietnam 0.000000
15533 2019 Bangladesh 0.000000
15544 2019 Brazil 0.000000
15561 2019 China 0.000000
15598 2019 France 0.000000
15603 2019 Germany 0.000000
15626 2019 India 0.000000
15627 2019 Indonesia 0.000000
15672 2019 Mexico 0.000000
15693 2019 Nigeria 0.000000
15703 2019 Pakistan 0.000000
15759 2019 Turkey 0.000000
15766 2019 United Kingdom 0.000000
15767 2019 United States 0.000000
15773 2019 Vietnam 0.000000

840 rows × 3 columns

In [14]:
beddata = beddata.groupby(['country'])['totalbed'].max().reset_index()
bedperth=beddata.sort_values("totalbed", axis = 0, ascending = False,inplace=False, kind='quicksort', na_position='last')
bedperth
Out[14]:
country totalbed
4 Germany 11.5000
3 France 11.1000
11 United Kingdom 10.7000
12 United States 9.2000
8 Nigeria 6.3703
1 Brazil 4.9951
2 China 4.2000
13 Vietnam 3.8329
10 Turkey 2.7000
7 Mexico 1.7000
9 Pakistan 1.3000
6 Indonesia 1.2000
5 India 0.9000
0 Bangladesh 0.8000
In [15]:
figbed = go.Figure(go.Bar(
            x=bedperth.totalbed,
            y=bedperth.country,marker=dict(
          color='rgba(50, 171, 96, 0.6)'),
            orientation='h'))


figbed.update_layout(
    title="Hospital Beds per 1000 people (In Most populated country)",
    xaxis_title="Hospital Beds Per 1000 People",
    yaxis_title="Country",
    autosize=False,
    width=1200,
    height=700,
    font=dict(
        family='Courier New, monospace',
        size=18,
        color="#7f7f7f"
    )
)

figbed.add_annotation(
            x=5,
            y=0,
            text="<br> WHO <br>Recommended bed</b>")
figbed.update_annotations(dict(
            xref="x",
            yref="y",
            showarrow=True,
            font=dict(
            family="Courier New, monospace",
            size=16,
            color="#FF0000"
            ),
        align="center",
        arrowhead=2,
        arrowsize=1,
        arrowwidth=2,
        arrowcolor="#000011",
        ax=0,
        ay=-400,
        bordercolor="#c7c7c7",
        borderwidth=2,
        borderpad=4,
        bgcolor="#ffffff",
        opacity=0.8
))


figbed.show()
In [16]:
# Physician bed per 1000 people
physician=pd.read_csv("physician.csv",engine='python')
In [17]:
phdata = physician.rename(
        columns = {'Country Name':'country'
                   ,'Country Code':'countrycode'
                   ,'Indicator Name':'Indname'
                   ,'Indicator Code':'Indcode'
                   }
        )

phdata = phdata.melt(id_vars = ['country','countrycode','Indname','Indcode']
                 ,var_name = 'year_RAW'
                 ,value_name = 'totalph'
                 )
phdata.isnull().sum()
Out[17]:
country            0
countrycode        0
Indname            0
Indcode            0
year_RAW           0
totalph        11279
dtype: int64
In [18]:
phdata['totalph'] = phdata['totalph'] .fillna(0)

array = ['China','United States','Italy','Spain','Germany',
        'Russian Federation','France','United Kingdom','Turkey','Brazil']
phdata = phdata[phdata.country.isin(array)]
In [19]:
physiciandata = phdata.groupby(['country'])['totalph'].max().reset_index()
physicianper1000=physiciandata.sort_values("totalph", axis = 0, ascending = True,inplace=False, kind='quicksort', na_position='last')
#physicianpersort=physicianper1000.nsmallest(5, 'totalph')
physicianper1000.head()
Out[19]:
country totalph
7 Turkey 1.7651
1 China 1.7855
0 Brazil 2.1502
8 United Kingdom 2.8058
9 United States 2.9401
In [20]:
marker_offset = 0.04

def offset_signal(signal, marker_offset):
    if abs(signal) <= marker_offset:
        return 0
    return signal - marker_offset if signal > 0 else signal + marker_offset

data = [
    go.Scatter(
        x=physicianper1000.country,
        y=physicianper1000.totalph,
        mode='lines+markers',
        marker=dict(color='red')
    )
]

# Use the 'shapes' attribute from the layout to draw the vertical lines
layout = go.Layout(
    shapes=[dict(
        type='line',
        xref='x',
        yref='y',
        x0=i,
        y0=0,
        x1=i,
        y1=offset_signal(len(physicianper1000), marker_offset),
        line=dict(
            color='grey',
            width=1
        )
    ) for i in range(len(physicianper1000))],
    title='Physicians per 1000 people (In Worst Affected country)',
     xaxis_title ="Country",
     yaxis_title ="Physicians per 1000 people",
    autosize=False,
    width=1100,
    height=700,
    font=dict(
        family='Courier New, monospace',
        size=18,
        color="#7f7f7f"
    )

)


# Plot the chart
figphysician = go.Figure(data, layout)
figphysician.show()
In [21]:
# covid 19 comparision with different dieases

ebola = pd.read_csv("ebola.csv", parse_dates=['Date'])

# selecting important columns only
ebola = ebola[['Date', 'Country', 'No. of confirmed, probable and suspected cases',
                     'No. of confirmed, probable and suspected deaths']]
# renaming columns
ebola.columns = ['Date', 'Country', 'Cases', 'Deaths']
ebola.head()

ebola = ebola.groupby(['Date', 'Country'])['Cases', 'Deaths']
ebola = ebola.sum().reset_index()
ebola['Cases'] = ebola['Cases'].fillna(0)
ebola['Deaths'] = ebola['Deaths'].fillna(0)

# latest
ebolalatest = ebola[ebola['Date'] == max(ebola['Date'])].reset_index()
ebola_data = ebolalatest.groupby('Country')['Cases', 'Deaths'].sum().reset_index()
ebola_data.head()
Out[21]:
Country Cases Deaths
0 Guinea 3804.0 2536.0
1 Italy 1.0 0.0
2 Liberia 10680.0 4813.0
3 Mali 8.0 6.0
4 Nigeria 20.0 8.0
In [22]:
# Sars Data

sars = pd.read_csv("sars.csv", parse_dates=['Date'])
# selecting important columns only
sars = sars[['Date', 'Country', 'Cumulative number of case(s)',  'Number of deaths', 'Number recovered']]
sars.columns = ['Date', 'Country', 'Cases', 'Deaths', 'Recovered']

sars = sars.groupby(['Date', 'Country'])['Cases', 'Deaths', 'Recovered']
sars = sars.sum().reset_index()

saralatest = sars[sars['Date'] == max(sars['Date'])].reset_index()
sars_data = saralatest.groupby('Country')['Cases', 'Deaths', 'Recovered'].sum().reset_index()
sars_data.head()
Out[22]:
Country Cases Deaths Recovered
0 Brazil 1 0 1
1 Canada 250 38 194
2 China 5327 348 4941
3 Colombia 1 0 1
4 Finland 1 0 1
In [23]:
# comparision of above three

covid_cases = sum(countrywise['confirmed'])
covid_deaths = sum(countrywise['deaths'])
covid_country = len(countrywise['country'].value_counts())

ebola_cases = sum(ebola_data['Cases'])
ebola_deaths = sum(ebola_data['Deaths'])
ebola_country = len(ebola_data['Country'].value_counts())

sars_cases = sum(sars_data['Cases'])
sars_deaths = sum(sars_data['Deaths'])
sars_country = len(sars_data['Country'].value_counts())
In [24]:
total_comarision=pd.DataFrame(columns=['Pandemic','start_year','end_year','confirmed','death','no_country','Fertility'],index=None)

total_comarision.Pandemic=['COVID-19', 'SARS', 'EBOLA','MERS']
total_comarision.start_year=[2019, 2003, 2014, 2012]
total_comarision.end_year=[2020, 2004, 2016, 2017]
total_comarision.confirmed=[covid_cases, sars_cases, ebola_cases,2494]
total_comarision.death=[covid_deaths, sars_deaths, ebola_deaths,858]
total_comarision.no_country=[covid_country, ebola_country, sars_country,27]
total_comarision.Fertility = round((total_comarision['death']/total_comarision['confirmed'])*100, 2)
total_comarision.to_csv('comarision.csv')

total_comarision.head()
Out[24]:
Pandemic start_year end_year confirmed death no_country Fertility
0 COVID-19 2019 2020 3533230.0 248455.0 222 7.03
1 SARS 2003 2004 8432.0 813.0 10 9.64
2 EBOLA 2014 2016 28642.0 11319.0 31 39.52
3 MERS 2012 2017 2494.0 858.0 27 34.40
In [25]:
from plotly.subplots import make_subplots
import plotly.graph_objects as go


total_comarision=total_comarision.sort_values('confirmed',ascending=False)
total_comarision=total_comarision.sort_values('death',ascending=False)
stages = total_comarision.Pandemic
df_mtl = pd.DataFrame(dict(number=total_comarision.confirmed, stage=stages))
df_mtl['Category'] = 'Confirmed'
df_toronto = pd.DataFrame(dict(number=total_comarision.death, stage=stages))
df_toronto['Category'] = 'Death'

df = pd.concat([df_mtl, df_toronto], axis=0)
fig_confirm = px.funnel(df, x='number', y='stage', color='Category',title="COVID-19 with other known epedemics")
fig_confirm.show()
In [26]:
# Test performed by countryies per 1000 people

testing=pd.read_csv("test.csv")

testing = testing.rename(
        columns = {'Total tests per thousand':'TotalTest'
                   }
        )
testing['Date'] = pd.to_datetime(testing['Date'], errors='coerce')
testing.head()
Out[26]:
Entity Code Date TotalTest
0 Argentina ARG 2020-04-08 0.295
1 Argentina ARG 2020-04-09 0.329
2 Argentina ARG 2020-04-10 0.362
3 Argentina ARG 2020-04-11 0.399
4 Argentina ARG 2020-04-13 0.437
In [27]:
array = ['United States','Italy','France','United Kingdom','South Korea','Germany']
testing = testing[testing.Entity.isin(array)]

figtesting = px.scatter(testing, x='Date', y='TotalTest', color='Entity',title='Test Performed per 1000 people')
figtesting.show()
In [28]:
#read csv file
df = pd.read_csv('covid_19_data.csv')
df = df.rename(
        columns = {'Country/Region':'Country'
                   }
        )

df.head()
Out[28]:
SNo ObservationDate Province/State Country Last Update Confirmed Deaths Recovered
0 1 22-01-2020 Anhui Mainland China 1/22/2020 17:00 1 0 0
1 2 22-01-2020 Beijing Mainland China 1/22/2020 17:00 14 0 0
2 3 22-01-2020 Chongqing Mainland China 1/22/2020 17:00 6 0 0
3 4 22-01-2020 Fujian Mainland China 1/22/2020 17:00 1 0 0
4 5 22-01-2020 Gansu Mainland China 1/22/2020 17:00 0 0 0
In [29]:
# find the active cases from the dataset and save in new column Active

df['Active'] = df['Confirmed'] - df['Deaths'] - df['Recovered']
In [30]:
# Select countries and apply in new dataframe

ar1 = ['Mainland China']
df1 = df[df.Country.isin(ar1)]
ar2 = ['South Korea']
df2 = df[df.Country.isin(ar2)]
ar3 = ['France']
df3 = df[df.Country.isin(ar3)]
ar4 = ['Italy']
df4 = df[df.Country.isin(ar4)]
ar5 = ['US']
df5 = df[df.Country.isin(ar5)]
ar6 = ['Spain']
df6 = df[df.Country.isin(ar6)]
ar7 = ['Germany']
df7 = df[df.Country.isin(ar7)]
In [31]:
#sum of the all active cases as per date
df1 = df1.groupby('ObservationDate')['Confirmed', 'Deaths', 'Recovered', 'Active'].sum().reset_index()
df2 = df2.groupby('ObservationDate')['Confirmed', 'Deaths', 'Recovered', 'Active'].sum().reset_index()
df3 = df3.groupby('ObservationDate')['Confirmed', 'Deaths', 'Recovered', 'Active'].sum().reset_index()
df4 = df4.groupby('ObservationDate')['Confirmed', 'Deaths', 'Recovered', 'Active'].sum().reset_index()
df5 = df5.groupby('ObservationDate')['Confirmed', 'Deaths', 'Recovered', 'Active'].sum().reset_index()
df6 = df6.groupby('ObservationDate')['Confirmed', 'Deaths', 'Recovered', 'Active'].sum().reset_index()
df7 = df7.groupby('ObservationDate')['Confirmed', 'Deaths', 'Recovered', 'Active'].sum().reset_index()
In [32]:
#select specific dates for plotting 

array1 = ['22-01-2020', '30-01-2020', '10-02-2020', '20-02-2020', '29-02-2020', '10-03-2020',
         '20-03-2020', '30-03-2020','10-04-2020', '20-04-2020', '30-04-2020']
df1 = df1[df1.ObservationDate.isin(array1)]
df2 = df2[df2.ObservationDate.isin(array1)]
df3 = df3[df3.ObservationDate.isin(array1)]
df4 = df4[df4.ObservationDate.isin(array1)]
df5 = df5[df5.ObservationDate.isin(array1)]
df6 = df6[df6.ObservationDate.isin(array1)]
df7 = df7[df7.ObservationDate.isin(array1)]
df1
Out[32]:
ObservationDate Confirmed Deaths Recovered Active
30 10-02-2020 42306 1011 3917 37378
31 10-03-2020 80757 3136 60106 17515
32 10-04-2020 81907 3336 77472 1099
60 20-02-2020 74999 2236 18002 54761
61 20-03-2020 80977 3249 71158 6570
62 20-04-2020 82747 4632 77093 1022
66 22-01-2020 547 17 28 502
95 29-02-2020 79251 2835 39279 37137
98 30-01-2020 8124 171 135 7818
99 30-03-2020 81478 3304 75784 2390
100 30-04-2020 82874 4633 77642 599
In [33]:
#sort the values as per confirmed

df1 = df1.sort_values(by='Confirmed')
df2 = df2.sort_values(by='Confirmed')
df3 = df3.sort_values(by='Confirmed')
df4 = df4.sort_values(by='Confirmed')
df5 = df5.sort_values(by='Confirmed')
df6 = df6.sort_values(by='Confirmed')
df7 = df7.sort_values(by='Confirmed')
df1
Out[33]:
ObservationDate Confirmed Deaths Recovered Active
66 22-01-2020 547 17 28 502
98 30-01-2020 8124 171 135 7818
30 10-02-2020 42306 1011 3917 37378
60 20-02-2020 74999 2236 18002 54761
95 29-02-2020 79251 2835 39279 37137
31 10-03-2020 80757 3136 60106 17515
61 20-03-2020 80977 3249 71158 6570
99 30-03-2020 81478 3304 75784 2390
32 10-04-2020 81907 3336 77472 1099
62 20-04-2020 82747 4632 77093 1022
100 30-04-2020 82874 4633 77642 599
In [34]:
import plotly.graph_objs as go

figActive = go.Figure()

figActive.add_trace(go.Scatter(x=df1['ObservationDate'], y=df1['Active'], name = 'China',
                     line = {'width': 2, 'color': 'black'}))
figActive.add_trace(go.Scatter(x=df3['ObservationDate'], y=df3['Active'], name = 'France',
                    line = {'width': 2, 'color': 'Blue'}))
figActive.add_trace(go.Scatter(x=df4['ObservationDate'], y=df4['Active'], name = 'Italy',
                    line = {'width': 2, 'color': 'red'}))
figActive.add_trace(go.Scatter(x=df5['ObservationDate'], y=df5['Active'], name = 'USA',
                   line = {'width': 2, 'color': 'green'}))
figActive.add_trace(go.Scatter(x=df6['ObservationDate'], y=df6['Active'], name = 'Spain',
                    line = {'width': 2, 'color': 'orange'}))
figActive.add_trace(go.Scatter(x=df7['ObservationDate'], y=df7['Active'], name = 'Germany',
                    line = {'width': 2, 'color': 'yellow'}))



figActive.update_layout(
    #yaxis=dict(range=[0,12000]),
    xaxis = dict(
        tickmode = 'array',
        tickvals = ['22-01-2020', '30-01-2020', '10-02-2020', '20-02-2020', '29-02-2020', '10-03-2020',
         '20-03-2020', '30-03-2020','10-04-2020', '20-04-2020', '30-04-2020'],
        ticktext = ['22 Jan','30 Jan','10 Feb','20 Feb','29 Feb','10 Mar','20 Mar','30 Mar','30 Mar', '10 Apr','20 Apr','30 Apr'],
        tickangle=360
    ),
    autosize=False,
    width=1300,
    height=700,
    xaxis_title="<b> Dates </b> ",
    yaxis_title="<b> Number of active cases in thousand </b>",
    font=dict(
        family="Courier New, monospace",
        size=18,
        color="#000000"
    )
)

figActive.add_annotation(
            x=6.05,
            y=1000,
            ax=0,
            ay=-360,
            text="<br> USA <br>on 20 March </b>")
figActive.add_annotation(
            x=5.60,
            y=5500,
            ax=0,
            ay=-200,
            text="<br> France <br>16th March </b>")
figActive.add_annotation(
            x=5.00,
            y=1000,
            ax=0,
            ay=-290,
            text="<br> Italy <br>10th March </b>")
figActive.add_annotation(
            x=7.15,
            y=70000,
            ax=0,
            ay=-250,
            text="<br> Germany <br>2nd April </b>")
figActive.add_annotation(
            x=0.10,
            y=1000,
            ax=0,
            ay=-250,
            text="<br> China <br>23rd January</b>")

figActive.add_annotation(
            x=5.40,
            y=5500,
            ax=0,
            ay=-450,
            text="<br> Spain <br>14th March</b>"),

figActive.update_annotations(dict(
            xref="x",
            yref="y",
            showarrow=True,
            font=dict(
            family="Courier New, monospace",
            size=16,
            color="#000000"
            ),
        align="center",
        arrowhead=2,
        arrowsize=1,
        arrowwidth=2,
        arrowcolor="#000011",
        bordercolor="#c7c7c7",
        borderwidth=2,
        borderpad=4,
        bgcolor="#ffffff",
        opacity=0.8
))
In [35]:
# minus below row from above row for the date wise cases
df1['new'] = df1.Confirmed.diff()
df2['new'] = df2.Confirmed.diff()
df3['new'] = df3.Confirmed.diff()
df4['new'] = df4.Confirmed.diff()
df5['new'] = df5.Confirmed.diff()
df6['new'] = df6.Confirmed.diff()
df7['new'] = df7.Confirmed.diff()
In [36]:
# convert into positive number
df1['new'] = df1['new'].abs()
df2['new'] = df2['new'].abs()
df3['new'] = df3['new'].abs()
df4['new'] = df4['new'].abs()
df5['new'] = df5['new'].abs()
df6['new'] = df6['new'].abs()
df7['new'] = df7['new'].abs()
In [37]:
# now filling epmty space with cases
df1["new"].fillna("1")
df2["new"].fillna("1")
df3["new"].fillna("5")
df4["new"].fillna("3")
df5["new"].fillna("1")
df6["new"].fillna("2")
df7["new"].fillna("4")
Out[37]:
92        4
30       10
60        2
89       63
31     1378
61    18391
93    47037
32    55286
62    24894
94    15944
Name: new, dtype: object
In [38]:
import plotly.graph_objects as go # or plotly.express as px
figConfirmed = go.Figure()
figConfirmed.add_trace(go.Scatter(x=df1['ObservationDate'], y=df1['new'], name = 'China',
                     line = {'width': 2, 'color': 'black'}))
figConfirmed.add_trace(go.Scatter(x=df3['ObservationDate'], y=df3['new'], name = 'France',
                    line = {'width': 2, 'color': 'Blue'}))
figConfirmed.add_trace(go.Scatter(x=df4['ObservationDate'], y=df4['new'], name = 'Italy',
                    line = {'width': 2, 'color': 'red'}))
figConfirmed.add_trace(go.Scatter(x=df6['ObservationDate'], y=df6['new'], name = 'Spain',
                    line = {'width': 2, 'color': 'orange'}))
figConfirmed.add_trace(go.Scatter(x=df7['ObservationDate'], y=df7['new'], name = 'Germany',
                    line = {'width': 2, 'color': 'yellow'}))

figConfirmed.update_layout(
    #yaxis=dict(range=[0,12000
    xaxis = dict(
        tickmode = 'array',
        tickvals = ['22-01-2020', '30-01-2020', '10-02-2020', '20-02-2020', '29-02-2020', '10-03-2020',
         '20-03-2020', '30-03-2020','10-04-2020', '20-04-2020', '30-04-2020'],
        ticktext = ['22 Jan','30 Jan','10 Feb','20 Feb','29 Feb','10 Mar','20 Mar','30 Mar','30 Mar', '10 Apr','20 Apr','30 Apr'],
        tickangle=360
    ),
    autosize=False,
    width=1300,
    height=700,
    xaxis_title="<b> Dates </b> ",
    yaxis_title="<b> Number of Confirmed Cases in Thousand </b>",
    font=dict(
        family="Courier New, monospace",
        size=18,
        color="#000000"
    )
)

figConfirmed.add_annotation(
            x=0.10,
            y=1000,
            ax=0,
            ay=-200,
            text="<br> China <br>23rd January")
figConfirmed.add_annotation(
            x=5.65,
            y=6500,
            ax=0,
            ay=-200,
            text="<br> France <br>16th March")
figConfirmed.add_annotation(
            x=7.25,
            y=49000,
            ax=0,
            ay=-200,
            text="<br> Germany <br>2nd April")
figConfirmed.add_annotation(
            x=5.00,
            y=7300,
            ax=0,
            ay=-290,
            text="<br> Italy <br>10th March")
figConfirmed.add_annotation(
            x=5.45,
            y=8200,
            ax=0,
            ay=-380,
            text="<br> Spain <br>14th March"),
figConfirmed.update_annotations(dict(
            xref="x",
            yref="y",
            arrowcolor = "#000011",
            arrowhead=2,
            arrowsize=1,
            arrowwidth=2,
            bordercolor="#c7c7c7",
            borderwidth=2,
            borderpad=4,
            bgcolor="#ffffff",
            opacity=0.8
))
In [39]:
#deaths pr day and storing in onther column
df1['new_deaths'] = df1.Deaths.diff()
df2['new_deaths'] = df2.Deaths.diff()
df3['new_deaths'] = df3.Deaths.diff()
df4['new_deaths'] = df4.Deaths.diff()
df5['new_deaths'] = df5.Deaths.diff()
df6['new_deaths'] = df6.Deaths.diff()
df7['new_deaths'] = df7.Deaths.diff()
df3
Out[39]:
ObservationDate Confirmed Deaths Recovered Active new new_deaths
96 30-01-2020 5 0 0 5 NaN NaN
30 10-02-2020 11 0 0 11 6.0 0.0
60 20-02-2020 12 1 4 7 1.0 1.0
93 29-02-2020 100 2 12 86 88.0 1.0
31 10-03-2020 1784 33 12 1739 1684.0 31.0
61 20-03-2020 12632 450 12 12170 10848.0 417.0
97 30-03-2020 45170 3030 7964 34176 32538.0 2580.0
32 10-04-2020 125931 13215 25195 87521 80761.0 10185.0
62 20-04-2020 156480 20292 38036 98152 30549.0 7077.0
98 30-04-2020 167299 24410 50380 92509 10819.0 4118.0
In [40]:
# filling empty values as per dataset
df1["new_deaths"].fillna("17")
df2["new_deaths"].fillna("0")
df3["new_deaths"].fillna("0")
df4["new_deaths"].fillna("0")
df5["new_deaths"].fillna("0")
df6["new_deaths"].fillna("0")
df7["new_deaths"].fillna("0")
df3
Out[40]:
ObservationDate Confirmed Deaths Recovered Active new new_deaths
96 30-01-2020 5 0 0 5 NaN NaN
30 10-02-2020 11 0 0 11 6.0 0.0
60 20-02-2020 12 1 4 7 1.0 1.0
93 29-02-2020 100 2 12 86 88.0 1.0
31 10-03-2020 1784 33 12 1739 1684.0 31.0
61 20-03-2020 12632 450 12 12170 10848.0 417.0
97 30-03-2020 45170 3030 7964 34176 32538.0 2580.0
32 10-04-2020 125931 13215 25195 87521 80761.0 10185.0
62 20-04-2020 156480 20292 38036 98152 30549.0 7077.0
98 30-04-2020 167299 24410 50380 92509 10819.0 4118.0
In [44]:
import plotly.graph_objects as go # or plotly.express as px
from plotly.subplots import make_subplots

figDeaths = go.Figure() # or any Plotly Express function e.g. px.bar(...)
figDeaths = make_subplots(rows=3, cols=2, shared_yaxes=False, x_title='Dates', 
                    y_title='Number of death in thousand ',
                    subplot_titles=("China Announced Lockdown On 23rd January ","USA Announced Lockdown On 20th March", "France 16th Announced Lockdown On March",
                                    "Italy Announced Lockdown On 10th March",
                                    "Spain Announced Lockdown On 14th March",
                                    "Germany Announced Lockdown On 2nd April"))
figDeaths.add_trace(go.Scatter(x=df1['ObservationDate'], y=df1['new_deaths'], name = 'China',
                     line = {'width': 2, 'color': 'black'}),row=1, col=1)
figDeaths.add_trace(go.Scatter(x=df5['ObservationDate'], y=df5['new_deaths'], name = 'USA',
                    line = {'width': 2, 'color': 'green'}),row=1, col=2)
figDeaths.add_trace(go.Scatter(x=df3['ObservationDate'], y=df3['new_deaths'], name = 'France',
                    line = {'width': 2, 'color': 'Blue'}),row=2, col=1)
figDeaths.add_trace(go.Scatter(x=df4['ObservationDate'], y=df4['new_deaths'], name = 'Italy',
                    line = {'width': 2, 'color': 'red'}),row=2, col=2)
figDeaths.add_trace(go.Scatter(x=df6['ObservationDate'], y=df6['new_deaths'], name = 'Spain',
                    line = {'width': 2, 'color': 'orange'}),row=3, col=1)
figDeaths.add_trace(go.Scatter(x=df7['ObservationDate'], y=df7['new_deaths'], name = 'Germany',
                    line = {'width': 2, 'color': 'green'}),row=3, col=2)

figDeaths.update_layout(height=1000, width=1300)
#fig.update_xaxes(tickangle=340)
figDeaths.update_layout(
    xaxis = dict(
        tickmode = 'array',
        tickvals = ['22-01-2020', '30-01-2020', '10-02-2020', '20-02-2020', '29-02-2020', '10-03-2020',
         '20-03-2020', '30-03-2020','10-04-2020', '20-04-2020', '30-04-2020'],
        ticktext = ['22 Jan','30 Jan','10 Feb','20 Feb','29 Feb','10 Mar','20 Mar','30 Mar','30 Mar', '10 Apr','20 Apr','30 Apr'],
        tickangle=360
    ),
    xaxis2 = dict(
        tickmode = 'array',
        tickvals = ['22-01-2020', '30-01-2020', '10-02-2020', '20-02-2020', '29-02-2020', '10-03-2020',
         '20-03-2020', '30-03-2020','10-04-2020', '20-04-2020', '30-04-2020'],
        ticktext = ['22 Jan','30 Jan','10 Feb','20 Feb','29 Feb','10 Mar','20 Mar','30 Mar','30 Mar', '10 Apr','20 Apr','30 Apr'],
        tickangle=360
    ),
    xaxis3 = dict(
        tickmode = 'array',
        tickvals = ['22-01-2020', '30-01-2020', '10-02-2020', '20-02-2020', '29-02-2020', '10-03-2020',
         '20-03-2020', '30-03-2020','10-04-2020', '20-04-2020', '30-04-2020'],
        ticktext = ['22 Jan','30 Jan','10 Feb','20 Feb','29 Feb','10 Mar','20 Mar','30 Mar','30 Mar', '10 Apr','20 Apr','30 Apr'],
        tickangle=360
    ),
    xaxis4 = dict(
        tickmode = 'array',
        tickvals = ['22-01-2020', '30-01-2020', '10-02-2020', '20-02-2020', '29-02-2020', '10-03-2020',
         '20-03-2020', '30-03-2020','10-04-2020', '20-04-2020', '30-04-2020'],
        ticktext = ['22 Jan','30 Jan','10 Feb','20 Feb','29 Feb','10 Mar','20 Mar','30 Mar','30 Mar', '10 Apr','20 Apr','30 Apr'],
        tickangle=360
    ),
    xaxis5 = dict(
        tickmode = 'array',
        tickvals = ['22-01-2020', '30-01-2020', '10-02-2020', '20-02-2020', '29-02-2020', '10-03-2020',
         '20-03-2020', '30-03-2020','10-04-2020', '20-04-2020', '30-04-2020'],
        ticktext = ['22 Jan','30 Jan','10 Feb','20 Feb','29 Feb','10 Mar','20 Mar','30 Mar','30 Mar', '10 Apr','20 Apr','30 Apr'],
        tickangle=360
    ),
    xaxis6 = dict(
        tickmode = 'array',
        tickvals = ['22-01-2020', '30-01-2020', '10-02-2020', '20-02-2020', '29-02-2020', '10-03-2020',
         '20-03-2020', '30-03-2020','10-04-2020', '20-04-2020', '30-04-2020'],
        ticktext = ['22 Jan','30 Jan','10 Feb','20 Feb','29 Feb','10 Mar','20 Mar','30 Mar','30 Mar', '10 Apr','20 Apr','30 Apr'],
        tickangle=360
    )
)
In [45]:
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(children=[
    html.H1(children='Covid 19 Dataset Visualization'),

    html.Div(children=  html.H3(children='Section 1 (Analysis of Covid 19 Infection across the globe)')),
     dcc.Graph(
        id='worldgraph',
        figure=figch ),
      html.Div(children='Above graph provides the timeline of the COVID 19 Pandemic around the globe. The information displayed is the confirmed cases, recovered and deaths. Also the spread has been  mapped since start.',
              style={
        'color': '#0000A0'}),

     dcc.Graph(
        id='casepercountry',
        figure=fig ),
    
     html.Div(children=  html.H3(children='Section 2(Covid 19 states comparision )')),
    dcc.Graph(
        id='comparision_confirm',
        figure=fig_confirm),
     html.Div(children='Above graph compares COVID-19 with other known epedemics. The mortality rate for  COVID-19 is 7.03, SARS 9.64, EBOLA 39.52 and MERS 34.40. Although mortality rate of COVID-19 is low but what makes it dangerous is that it his highly communicable with R0 factor between 2 - 2.5, by comparison R0 for common cold is 1.5.',
              style={
        'color': '#0000A0'}),
    dcc.Graph(
        id='age_sexwise',
        figure=figage_gender),
    
   html.Div(children=  html.H3(children='Section 3(Hospital Bed And physician capacity across the globe )')),

    dcc.Graph(
        id='hospital_bed',
        figure=figbed),
    
      html.Div(children='Above graph displays the available hospital beds per 1000 popullation in 14 most populated countries in world. Note: WHO recommended ratio is 5 beds per 1000 population.',
              style={
        'color': '#0000A0'}),
    
    dcc.Graph(
        id='physician',
        figure=figphysician),
    html.Div(children='Above graph provides the analysis of the No. Of physicians per 1000 personnel in 10 countries that are severely affected by COVID-19',
              style={
        'color': '#0000A0'}),
    
    html.Div(children=  html.H3(children='Section 4(Covid 19 Testing)')),
    dcc.Graph(
        id='testing',
        figure=figtesting),
    html.Div(children='Above graph shows the COVID-19 testing per 1000 person in different countries.',
              style={
        'color': '#0000A0'}),
    
    html.Div(children=  html.H3(children='Section 5(Covid 19 Flattening Curve)')),
   
    html.H3(children='Active cases in worst hit countries '),
    html.Div(children='''Below graph displays the flattening of curve after social
    distancing measure are applied by different countries. The total no. Of active cases flatten and then decrease gradually.'''),
    
    dcc.Graph(id = 'Active Graph', figure=figActive),
    
    html.H3(children='Confirmed cases per country '),
    html.Div(children='''Below graph shows decrease in the new covid-19 cases after social distancing measures are inacted.'''),
    dcc.Graph(id = 'Confirmed Graph', figure=figConfirmed),
    
    html.H3(children='''Multiple Subplots with Shared Y-Axes'''),
    html.H3(children='''Below subplots shows the decline in death rate after lockdown was initiated in each country this was due to 
    the decrease in new cases which resulted in Medical Facilities not being overburdened.'''),
    
    dcc.Graph(id = 'Death Graph', figure=figDeaths),
    
               
])

if __name__ == '__main__':
    app.run_server(debug=False)
Dash is running on http://127.0.0.1:8050/

Dash is running on http://127.0.0.1:8050/

 * Serving Flask app "__main__" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)
127.0.0.1 - - [25/Oct/2020 16:07:18] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [25/Oct/2020 16:07:19] "GET /_dash-dependencies HTTP/1.1" 200 -
127.0.0.1 - - [25/Oct/2020 16:07:19] "GET /_dash-layout HTTP/1.1" 200 -
In [ ]: